﻿using MyMD5;

namespace MyRegister
{
    /// <summary>
    /// 验证获取注册码
    /// </summary>
    public class VerifyRegisterCode
    {
        /// <summary>
        /// 检测注册码有效性
        /// </summary>
        /// <param name="serialNumber"></param>
        /// <param name="regCode"></param>
        /// <returns></returns>
        public static bool CheckRegisterCode(string serialNumber, string regCode)
        {
            string tempReg = GetRegisterCode(serialNumber);
            return regCode == tempReg;
        }

        /// <summary>
        /// 获取注册码
        /// </summary>
        /// <param name="serialNumber"></param>
        /// <returns></returns>
        public static string GetRegisterCode(string serialNumber)
        {
            string strMNum = serialNumber;
            string tempReg = MyMd5Helper.Md5Encrypt(strMNum + "v1", "X2").Substring(1, 26);
            char[] array = tempReg.ToUpper().ToCharArray();
            string result = "";
            for (int i = 0; i < 5; i++)
            {
                for (int num = 5; num > 0; num--)
                {
                    result += array[num * 5 - i - 1];
                }
                if (i < 4)
                {
                    result += "-";
                }
            }
            return result;
        }
    }
}
